package com.taomus.browser;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.LocationEvent;
import org.eclipse.swt.browser.LocationListener;
import org.eclipse.swt.browser.OpenWindowListener;
import org.eclipse.swt.browser.StatusTextEvent;
import org.eclipse.swt.browser.StatusTextListener;
import org.eclipse.swt.browser.VisibilityWindowListener;
import org.eclipse.swt.browser.WindowEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.TouchEvent;
import org.eclipse.swt.events.TouchListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Scrollable;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class GeckoBrowser extends Browser {
	private static String basePath = System.getProperty("user.dir");
	private static String xulpath = basePath + "/xulrunner";
	private static String LASTLOC = "";
	private static Browser browser;
	private static String defualUrl = "about:blank";
	private static boolean jsenabled = true;
	static {
		ResourceBundle rb = ResourceBundle.getBundle("config");
		defualUrl = rb.getString("url");
		String xulpath1 = rb.getString("xulpath");
		if (xulpath1 != null && !xulpath1.equals("")) {
			xulpath = xulpath1;
		}
		System.setProperty("org.eclipse.swt.browser.XULRunnerPath", xulpath);
	}

	private GeckoBrowser(Composite parent, int style) {
		super(parent, style);
	}

	public GeckoBrowser(Composite parent) {
		super(parent, SWT.MOZILLA);
		Shell shell = (Shell) parent;
		shell.setImage(SWTResourceManager.getImage(shell.getClass(), "/logo.png"));
		browser = this;
		browser.setJavascriptEnabled(jsenabled);
		Menu menu = new Menu(parent);
		MenuItem mitem = new MenuItem(menu, SWT.PUSH);
		mitem.setText("刷新");
		mitem.addListener(SWT.Selection, event -> {
			MenuItem mi = (MenuItem) event.widget;
			if (mi.getText().trim().equals("刷新")) {
				this.refresh();
			}
		});
		this.setMenu(menu);
		init();
	}

	private void init() {
		this.setUrl(defualUrl);
		this.addCloseWindowListener(event -> {
			Browser browser1 = (Browser) event.widget;
		});
		this.addTitleListener(event -> {
			Browser browser1 = (Browser) event.widget;
			browser1.getShell().setText(event.title);
		});
		this.addLocationListener(new LocationListener() {
			
			@Override
			public void changing(LocationEvent arg0) { }
			
			@Override
			public void changed(LocationEvent arg0) {
				LASTLOC = arg0.location;
			}
		});
		this.addOpenWindowListener(new OpenWindowListener() {
			@Override
			public void open(WindowEvent arg0) {
				//处理新窗口 与新url
				arg0.browser = new GeckoBrowser(getParent());
				Display d = getParent().getShell().getDisplay();
				Shell shell = new Shell(d);
				shell.open();
			}
		});
	}

	public void setLeftInfo(Label label) {
		// 从浏览器状态中获取要跳转的URL
		this.addStatusTextListener(new StatusTextListener() {
			@Override
			public void changed(StatusTextEvent arg0) {
				String info = arg0.text;
				label.setText(info);
			}
		});
	
	}

	public void go(Button btn, Scrollable dest) {
		btn.setToolTipText("转到");
		btn.setText("→");
		if (dest instanceof Text) {
			Text text = (Text) dest;
			this.addLocationListener(new LocationListener() {

				@Override
				public void changing(LocationEvent arg0) {
				}

				@Override
				public void changed(LocationEvent arg0) {
					text.setText(arg0.location);
				}
			});
			btn.addListener(SWT.Selection, event -> {
				this.setUrl(text.getText());
			});

		}

	}

	private void browserButton(Button btn) {
		btn.addListener(SWT.Selection, event -> {
			Button btn1 = (Button) event.widget;
			switch (btn1.getData().toString()) {
			case "back":
				this.back();
				break;
			case "forward":
				this.forward();
				break;
			case "refresh":
				this.refresh();
				break;
			case "stop":
				this.stop();
				break;
			}
		});
	}

	public void back(Button btn) {
		btn.setData("back");
		btn.setText("◀");
		btn.setToolTipText("上一页");
		browserButton(btn);
	}

	public void forward(Button btn) {
		btn.setData("forward");
		btn.setText("▶");
		btn.setToolTipText("下一页");
		browserButton(btn);
	}

	public void refresh(Button btn) {
		btn.setData("refresh");
		btn.setText("刷新");
		btn.setToolTipText("刷新");
		browserButton(btn);
	}

	public void stop(Button btn) {
		btn.setData("stop");
		btn.setText("×");
		btn.setToolTipText("停止");
		browserButton(btn);
	}

	@Override
	protected void checkSubclass() {
	}
}
